import csv
from typing import List
import pymongo
from bson import ObjectId
from datetime import datetime
import uuid
import pandas as pd
from model.stock import Stock
from utils.databaseConfig import databaseConfig
from datetime import datetime

# 日期字符串
date_string = "2024-4-30"
# 指定日期格式
date_format = "%Y-%m-%d"


class StockConnector:
    def __init__(self) -> None:
        client = pymongo.MongoClient(databaseConfig.database_url)
        db = client[databaseConfig.database_name]
        self.collection = db["stock"]

  
    
stockConnector = StockConnector()
    # 使用pandas读取csv文件，转换日期列为datetime对象
df = pd.read_csv('上证指数历史数据.csv', encoding='utf8')
df['日期'] = pd.to_datetime(df['日期'], format='%Y-%m-%d')
df = df.rename(columns={'日期': 'date', '收盘': 'close', '开盘': 'open', '高': 'high', '低': 'low', '交易量': 'vol', '涨跌幅': 'change'})
df['change'] = df['change']/100
df['pre_close'] = 0
df['pct_chg'] = 0
df['amount'] = 0
stock_data = df.to_dict('records')
post_data = {
        'name': '上证指数',
        'stock_code': '000001',
        'data': stock_data
    }
stockConnector.collection.insert_one(post_data )
